package com.gamehouse.eggpoacher;


import com.gamehouse.eggpoacher.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import Models.*;

public class DataBaseAdapter {
	
	private static final String DATABASE_NAME = "eggpoacherDatabase20";
	
	
	private static final String TABLE_PLAYER = "PLAYER";//Table PLAYER
	private static final String KEY_PLAYER_ID = "_id"; 
	private static final String KEY_PLAYER_NAME = "Name";
	private static final String KEY_PLAYER_SCORE = "Score";
	
	private static final String TABLE_LEVEL = "LEVEL";//Table LEVEL
	private static final String KEY_LEVEL_ID = "_id"; 
	private static final String KEY_LEVEL_LEVELNUMBER = "LevelNumber";
	private static final String KEY_LEVEL_LEVELTIMER = "LevelTimer";
	private static final String KEY_LEVEL_EGGNUMBER = "EggNumber";
	
	private static final String TABLE_EGG = "EGG";//Table EGG
	private static final String KEY_EGG_ID = "_id"; 
	private static final String KEY_EGG_POINTS = "Points";
	private static final String KEY_EGG_IMAGE_ID = "Image";
	private static final String KEY_EGG_COLOR = "Color";
	
	private static final String TABLE_CURRENTGAME = "CURRENTGAME";//Table CURRENTGAME
	private static final String KEY_CURRENTGAME_ID = "_id"; 
	private static final String KEY_CURRENTGAME_LEVEL = "Level";
	private static final String KEY_CURRENTGAME_POINTS = "Points";
	
	private static final String TABLE_RELATION = "RELATION";//Table RELATION between egg and level
	private static final String KEY_RELATION_ID = "_id"; 
	private static final String KEY_RELATION_LevelID = "LevelID";
	private static final String KEY_RELATION_EggID = "EggID";
	private static final String KEY_RELATION_PERCENTAGE = "Percentage";
	private static final String KEY_RELATION_EggTimer = "EggTimer";
	
	private static final String TABLE_MAXSCORE = "MAXSCORE";//Table MAXScore ...guarda o score maximo de cada nivel
	private static final String KEY_MAXSCORE_ID = "_id"; 
	private static final String KEY_MAXSCORE_ActualMaxScore = "ActualMaxScore";
	private static final String KEY_MAXSCORE_ActualPlayerScore = "ActualPlayerScore";
	
	private static final String TABLE_OPTIONS = "OPTIONS";
	private static final String KEY_OPTIONS_ID = "_id";
	private static final String KEY_OPTIONS_SOUND = "Sound";
	private static final String KEY_OPTIONS_MUSIC = "Music";
	private static final String KEY_OPTIONS_VIBRATOR = "Vibrator";
	
	private static final String TAG = "DataBaseAdapter";
	private static final int DATABASE_VERSION = 1;
	
	//Querys de cria��o da base de dados
	private static final String TABLE_CREATE_PLAYER =
		"CREATE TABLE 'PLAYER' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'Name' VARCHAR NOT NULL , 'Score' INTEGER NOT NULL );";
	
	private static final String TABLE_CREATE_LEVEL =
		"CREATE TABLE 'LEVEL' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'LevelNumber' INTEGER NOT NULL , 'LevelTimer' INTEGER NOT NULL , 'EggNumber' INT NOT NULL )";
	private static final String TABLE_CREATE_EGG =
		"CREATE TABLE 'EGG' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'Points' INTEGER NOT NULL , 'Image' INTEGER NOT NULL , 'Color' VARCHAR NOT NULL )";
	private static final String TABLE_CREATE_CURRENTGAME =
		"CREATE TABLE 'CURRENTGAME' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'Level' INTEGER NOT NULL , 'Points' INTEGER NOT NULL )";
	private static final String TABLE_CREATE_RELATION =
		"CREATE TABLE 'RELATION' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'LevelID' INTEGER NOT NULL , 'EggID' INTEGER NOT NULL , 'Percentage' INTEGER NOT NULL , 'EggTimer' INTEGER NOT NULL )";
	
	private static final String TABLE_SAVEMAXSCORE_RUNTIME = 
		"CREATE TABLE 'MAXSCORE' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'ActualMaxScore' INTEGER NOT NULL, 'ActualPlayerScore' INTEGER NOT NULL );";
	
	private static final String TABLE_CREATE_OPTIONS = 
		"CREATE TABLE 'OPTIONS' " +
		"('_id' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 'Sound' INTEGER NOT NULL, 'Music' INTEGER NOT NULL, 'Vibrator' INTEGER NOT NULL );";
	
	
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DataBaseAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(ctx);
	
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			try {
				db.execSQL(TABLE_CREATE_PLAYER);
				db.execSQL(TABLE_CREATE_LEVEL);
				db.execSQL(TABLE_CREATE_EGG);
				db.execSQL(TABLE_CREATE_CURRENTGAME);			
				db.execSQL(TABLE_CREATE_RELATION);			
				db.execSQL(TABLE_SAVEMAXSCORE_RUNTIME);			
				db.execSQL(TABLE_CREATE_OPTIONS);
				
				
				//inserts dos Scores
				db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player1', '10000');");
				db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player2', '8000');");
				db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player3', '6000');");
				db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player4', '4000');");
				db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player5', '2000');");
				System.out.println("player inserido");
				//inserts dos niveis
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('1', '1000', '20');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('2', '975', '25');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('3', '950', '30');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('4', '925', '35');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('5', '900', '40');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('6', '875', '45');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('7', '850', '50');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('8', '825', '55');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('9', '800', '60');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('10', '775', '65');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('11', '750', '70');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('12', '725', '75');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('13', '700', '80');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('14', '650', '85');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('15', '600', '90');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('16', '550', '95');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('17', '500', '100');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('18', '450', '105');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('19', '400', '110');");
				db.execSQL("INSERT INTO 'LEVEL' ('LevelNumber', 'LevelTimer', 'EggNumber') VALUES ('20', '350', '105');");
				
				System.out.println("niveis criados");
				//insert dos ovos
				db.execSQL("INSERT INTO 'EGG' ('Points', 'Image', 'Color') VALUES ('100','" + R.drawable.ovo_branco  + "' , 'White');");
				db.execSQL("INSERT INTO 'EGG' ('Points', 'Image', 'Color') VALUES ('200','" + R.drawable.ovo_azul  + "' , 'Blue');");
				db.execSQL("INSERT INTO 'EGG' ('Points', 'Image', 'Color') VALUES ('-100','" + R.drawable.ovo_vermelho  + "' , 'Red');");
				db.execSQL("INSERT INTO 'EGG' ('Points', 'Image', 'Color') VALUES ('-500','" + R.drawable.ovo_preto  + "' , 'Black');");
				db.execSQL("INSERT INTO 'EGG' ('Points', 'Image', 'Color') VALUES ('1000','" + R.drawable.ovo_dourado  + "' , 'Gold');");
				System.out.println("ovos inseridos");
				
				//insere a nivel e o os pontos actuais
				db.execSQL("INSERT INTO 'CURRENTGAME' ('Level', 'Points') VALUES ('0','0');");	
				
				//insere dados realcionados com os ovos e niveis
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('1','1','100','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('1','2','0','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('1','3','0','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('1','4','0','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('1','5','0','3000');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('2','1','80','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('2','2','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('2','3','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('2','4','0','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('2','5','0','2500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('3','1','60','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('3','2','10','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('3','3','20','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('3','4','0','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('3','5','10','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('4','1','50','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('4','2','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('4','3','20','2400');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('4','4','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('4','5','10','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('5','1','51','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('5','2','10','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('5','3','10','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('5','4','20','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('5','5','9','1500');");
							
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('6','1','52','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('6','2','10','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('6','3','10','2000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('6','4','20','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('6','5','8','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('7','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('7','2','8','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('7','3','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('7','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('7','5','7','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('8','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('8','2','9','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('8','3','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('8','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('8','5','6','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('9','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('9','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('9','3','10','2500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('9','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('9','5','5','1500');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('10','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('10','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('10','3','10','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('10','4','16','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('10','5','4','900');");

				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('11','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('11','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('11','3','10','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('11','4','17','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('11','5','3','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('12','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('12','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('12','3','10','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('12','4','18','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('12','5','2','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('13','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('13','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('13','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('13','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('13','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('14','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('14','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('14','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('14','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('14','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('15','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('15','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('15','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('15','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('15','5','1','900');");
							
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('16','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('16','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('16','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('16','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('16','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('17','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('17','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('17','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('17','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('17','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('18','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('18','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('18','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('18','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('18','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('19','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('19','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('19','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('19','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('19','5','1','900');");
								
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('20','1','60','1500');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('20','2','10','1000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('20','3','14','2300');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('20','4','15','3000');");
				db.execSQL("INSERT INTO 'RELATION' ('LevelID', 'EggID', 'Percentage', 'EggTimer') VALUES ('20','5','1','900');");
				//insere os valores default
				db.execSQL("INSERT INTO MAXSCORE ('ActualMaxScore','ActualPlayerScore' ) VALUES ('0', '0');");
				db.execSQL("INSERT INTO OPTIONS ('Sound','Music','Vibrator' ) VALUES ('0', '0','0');");
				
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println("SQL EXCEPTION");
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");  
			db.execSQL("DROP TABLE IF EXISTS PLAYER;");
			db.execSQL("DROP TABLE IF EXISTS LEVEL; ");
			db.execSQL("DROP TABLE IF EXISTS EGG;");
			db.execSQL("DROP TABLE IF EXISTS CURRENTGAME;");
			db.execSQL("DROP TABLE IF EXISTS RELATION;");
			onCreate(db);
		}
	}
	
	//---opens the database---
	public DataBaseAdapter open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//---closes the database---
	public void close()
	{
		DBHelper.close();
	}
	
	// PLAYER TABLE ------------------------------------------------------------------
	public long insertPlayer(String name, long score)
	{
		ContentValues newScore = new ContentValues();
		newScore.put(KEY_PLAYER_NAME, name);
		newScore.put(KEY_PLAYER_SCORE, score);
		return db.insert(TABLE_PLAYER, null, newScore);
	}
	
	public boolean deletePlayer(long id)
	{
		return db.delete(TABLE_PLAYER, KEY_PLAYER_ID + "=" + id, null) > 0;
	}
		
	public Cursor getAllScores()
	{
		return db.query(TABLE_PLAYER, new String[] {KEY_PLAYER_ID, KEY_PLAYER_NAME, KEY_PLAYER_SCORE}, null, null, null, null, null);
	}
	
	public Cursor getTop5Scores()
	{
		return  db.rawQuery("SELECT Name, Score FROM PLAYER ORDER BY Score DESC", null);

	}
	
	//LEVEL TABLE
	public Cursor getLevelByLevelNumber (long level)
	{
		Cursor toReturn = db.query(true, TABLE_LEVEL, new String[] 
		                          {KEY_LEVEL_LEVELTIMER, KEY_LEVEL_EGGNUMBER}
		                          , KEY_LEVEL_LEVELNUMBER + "=" + level, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	//EGG TABLE
	public Cursor getEggByID (long id)
	{
		Cursor toReturn = db.query(true, TABLE_EGG, new String[] 
		                          {KEY_EGG_POINTS,KEY_EGG_IMAGE_ID, KEY_EGG_COLOR}
		                          , KEY_EGG_ID + "=" + id, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	public Cursor getAllEggs()
	{
		return db.query(TABLE_EGG, new String[] {KEY_EGG_ID, KEY_EGG_POINTS,KEY_EGG_IMAGE_ID, KEY_EGG_COLOR}, null, null, null, null, null);
	}
	
	public Cursor getAEggNameAndPoints()
	{
		return db.query(TABLE_EGG, new String[] {KEY_EGG_POINTS, KEY_EGG_COLOR}, null, null, null, null, null);
	}
	
	//TABLE RELATION
	public Cursor getRelationByLevelID (long id)
	{
		Cursor toReturn = db.query(true, TABLE_RELATION, new String[] 
		                          {KEY_RELATION_EggID, KEY_RELATION_PERCENTAGE, KEY_RELATION_EggTimer}
		                          , KEY_RELATION_LevelID + "=" + id, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	//CURRENTGAME TABLE
	public Cursor getCurrentGame ()
	{
		Cursor toReturn = db.query(true, TABLE_CURRENTGAME, new String[] 
		                          {KEY_CURRENTGAME_LEVEL,KEY_CURRENTGAME_POINTS }
		                          , KEY_EGG_ID + "=" + 1, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	public long insertCurrentGame(long levelNumber, long currentScore)
	{
		ContentValues newCurrentGame = new ContentValues();
		newCurrentGame.put(KEY_CURRENTGAME_LEVEL, levelNumber);
		newCurrentGame.put(KEY_CURRENTGAME_POINTS, currentScore);
		return db.insert(TABLE_CURRENTGAME, null, newCurrentGame);
	}
	
	public boolean deleteCurrentGame(long id)
	{
		return db.delete(TABLE_CURRENTGAME, KEY_CURRENTGAME_ID + "=" + id, null) > 0;
	}
	
	public boolean updateScore(long id, long levelNumber,long currentScore )
	{
		ContentValues updateCurrentGame = new ContentValues();
		updateCurrentGame.put(KEY_CURRENTGAME_LEVEL, levelNumber);
		updateCurrentGame.put(KEY_CURRENTGAME_POINTS, currentScore);
		
		return db.update(TABLE_CURRENTGAME, updateCurrentGame, KEY_CURRENTGAME_ID + "=" + id, null) > 0;
	}
	
	//delete table player e current Game
	public void resetScores()
	{
		db.execSQL("DELETE FROM PLAYER");
		updateScore(1,0,0);
		
		db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player1', '0');");
		db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player2', '0');");
		db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player3', '0');");
		db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player4', '0');");
		db.execSQL("INSERT INTO 'PLAYER' ('Name', 'Score') VALUES ('Player5', '0');");
		
	}
	
	//Tabela MaxScore -> so e nececario guardar um valor
	public Cursor getActualMaxScore ()
	{
		Cursor toReturn = db.query(true, "MAXSCORE", new String[] 
		                          {KEY_MAXSCORE_ActualMaxScore}
		                          , KEY_EGG_ID + "=" + 1, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	public Cursor getActualPlayerScore ()
	{
		Cursor toReturn = db.query(true, "MAXSCORE", new String[] 
		                          {KEY_MAXSCORE_ActualPlayerScore}
		                          , KEY_EGG_ID + "=" + 1, null, null, null, null, null);
		if (toReturn != null) {
			toReturn.moveToFirst();
		}
		return toReturn;
	}
	
	public boolean updateMaxScore(long newMaxScore )
	{
		ContentValues updateCurrentGame = new ContentValues();
		updateCurrentGame.put(KEY_MAXSCORE_ActualMaxScore, newMaxScore);
		
		return db.update(TABLE_MAXSCORE, updateCurrentGame, KEY_MAXSCORE_ID + "=" + 1, null) > 0;
	}
	
	public boolean updatePlayerScore (long newPlayerScore)
	{
		ContentValues updateCurrentGame = new ContentValues();
		updateCurrentGame.put(KEY_MAXSCORE_ActualPlayerScore, newPlayerScore);
		
		return db.update(TABLE_MAXSCORE, updateCurrentGame, KEY_MAXSCORE_ID + "=" + 1, null) > 0;
	}
	
    
	
	//metodo que devolve um inteiro->N� do ultimo nivel BASE DE DADOS
	public Cursor getLastLevelNumber()
	{
		return db.query(TABLE_LEVEL, 
		         new String[] {KEY_LEVEL_LEVELNUMBER}, null, null, null, null, null);	
	}
	
	//TABLE OPTIONS 
	//db.execSQL("INSERT INTO OPTIONS ('Sound','Music','Vibrator' ) 
	//VALUES ('0', '0','0');");
	
	public boolean updateOptions (int newSoundOption, int newMusicOption, int newVibratorOption )
	{
		ContentValues cv = new ContentValues();
		cv.put(KEY_OPTIONS_SOUND, newSoundOption);
		cv.put(KEY_OPTIONS_SOUND, newMusicOption);
		cv.put(KEY_OPTIONS_SOUND, newVibratorOption);		
		return db.update(TABLE_OPTIONS, cv, KEY_OPTIONS_ID + "=" + 1, null) > 0;
	}
	
	public Cursor getAllOptions()
	{
		return db.query(TABLE_OPTIONS, 
		         new String[] {KEY_OPTIONS_SOUND,KEY_OPTIONS_MUSIC,KEY_OPTIONS_VIBRATOR}, null, null, null, null, null);	
	}
	
	

	public Cursor getMusicOption()
	{
		return db.query(TABLE_OPTIONS, 
		         new String[] {KEY_OPTIONS_MUSIC}, null, null, null, null, null);	
	}
	
	public Cursor getVibratorOption ()
	{
		return db.query(TABLE_OPTIONS, 
		         new String[] {KEY_OPTIONS_VIBRATOR}, null, null, null, null, null);
	}
	
	public Cursor getSoundOption ()
	{
		return db.query(TABLE_OPTIONS, 
		         new String[] {KEY_OPTIONS_SOUND}, null, null, null, null, null);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
